package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.annotation.DataScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DzHyuserMapper;
import com.ruoyi.system.domain.DzHyuser;
import com.ruoyi.system.service.IDzHyuserService;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 参与人员Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-12
 */
@Service
public class DzHyuserServiceImpl implements IDzHyuserService 
{
    @Autowired
    private DzHyuserMapper dzHyuserMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询参与人员
     * 
     * @param cyId 参与人员主键
     * @return 参与人员
     */
    @Override
    public DzHyuser selectDzHyuserByCyId(Long cyId)
    {
        return dzHyuserMapper.selectDzHyuserByCyId(cyId);
    }

    /**
     * 查询参与人员列表
     * 
     * @param dzHyuser 参与人员
     * @return 参与人员
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<DzHyuser> selectDzHyuserList(DzHyuser dzHyuser)
    {

        //权限控制
        SysUser user=  SecurityUtils.getLoginUser().getUser();
        List<SysRole> listRole= sysRoleService.selectRolesByUserId(user.getUserId());
        int datascope = 5;
        for(SysRole role:listRole){
            if( role.getDataScope()!=null&&Integer.parseInt(role.getDataScope())<datascope){
                datascope=Integer.parseInt(role.getDataScope());
            }
        }
        dzHyuser.setDatascope(datascope);
        if(datascope==4){
            dzHyuser.setCheckDeptId(user.getDeptId());
        }
        if(datascope==5){
            DzHuiyuan huiyuan =  dzHuiyuanService.selectDzHuiyuanByDn(user.getPhonenumber());
            if(huiyuan!=null){
                dzHyuser.setCheckHuiyuanId(huiyuan.getId());
            }else{
                dzHyuser.setCheckHuiyuanId(0l);
            }

        }
        return dzHyuserMapper.selectDzHyuserList(dzHyuser);
    }

    @Override
    public List<DzHyuser> selectDzHyuserListWeb(DzHyuser dzHyuser)
    {
        return dzHyuserMapper.selectDzHyuserList(dzHyuser);
    }

    /**
     * 新增参与人员
     * 
     * @param dzHyuser 参与人员
     * @return 结果
     */
    @Override
    public int insertDzHyuser(DzHyuser dzHyuser)
    {
        return dzHyuserMapper.insertDzHyuser(dzHyuser);
    }

    /**
     * 修改参与人员
     * 
     * @param dzHyuser 参与人员
     * @return 结果
     */
    @Override
    public int updateDzHyuser(DzHyuser dzHyuser)
    {
        return dzHyuserMapper.updateDzHyuser(dzHyuser);
    }

    /**
     * 批量删除参与人员
     * 
     * @param cyIds 需要删除的参与人员主键
     * @return 结果
     */
    @Override
    public int deleteDzHyuserByCyIds(Long[] cyIds)
    {
        return dzHyuserMapper.deleteDzHyuserByCyIds(cyIds);
    }

    /**
     * 删除参与人员信息
     * 
     * @param cyId 参与人员主键
     * @return 结果
     */
    @Override
    public int deleteDzHyuserByCyId(Long cyId)
    {
        return dzHyuserMapper.deleteDzHyuserByCyId(cyId);
    }
}
